﻿namespace TrinityEngine
{
    public struct Rect : IEquatable<Rect>
    {
        public float x;

        public float y;

        public float width;

        public float height;

        public Vector2 bottomLeft => new Vector2(x, y);

        public Vector2 bottomRight => new Vector2(x + width, y);

        public Vector2 topLeft => new Vector2(x, y + height);

        public Vector2 topRight => new Vector2(x + width, y + height);

        /// <summary>
        /// bottom-left: x, y
        /// top-right: x + width, y + height
        /// </summary>
        public Rect(float x, float y, float width, float height)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public Rect(Vector2 location, Vector2 size)
        {
            x = location.x;
            y = location.y;
            width = size.x;
            height = size.y;
        }

        public bool Equals(Rect other)
        {
            return x == other.x && y == other.y && width == other.width && height == other.height;
        }

        public override bool Equals(object obj)
        {
            if (obj is Rect r)
            {
                return Equals(r);
            }

            return false;
        }

        public override int GetHashCode()
        {
            return x.GetHashCode() ^ y.GetHashCode() ^ width.GetHashCode() ^ height.GetHashCode();
        }

        public override string ToString()
        {
            return $"Rect({x}, {y}, {width}, {height})";
        }

        public static bool operator ==(Rect a, Rect b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(Rect a, Rect b)
        {
            return !a.Equals(b);
        }
    }
}
